﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.Checklist;
using PMPUtils.Form;
using PMPUtils.Resources;

namespace PMP.Checklist
{
    public partial class Dashboard : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormUtil.Authenticate(Session, Response);

            if (!IsPostBack)
            {

                txtTaskID.Text = Request.Params["TASKID"];

                try
                {
                    ChecklistOperator.RefreshAssignedGridView(grvAssigned, txtTaskID.Text.Trim());
                }
                catch (Exception ex)
                {
                    Messenger.Show(Response, "Error: " + ex.Message);
                    return;
                }
            }
        }

        protected void grvAvailable_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked 
            // by the user from the Rows collection.
            GridViewRow row = grvAvailable.Rows[index];

            if (e.CommandName.Equals("SelectChecklist"))
            {
                try
                {
                    String taskID = txtTaskID.Text.Trim();
                    String component = GridViewOperator.GetCellStringValue(grvAvailable, row, "component");


                    ChecklistOperator.AssignChecklist(taskID, component);
                }
                catch (Exception ex)
                {
                    Messenger.Show(Response, "Error: " + ex.Message);
                    return;
                }
            }

            RefreshForm();
        }

        protected void grvAssigned_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked 
            // by the user from the Rows collection.
            GridViewRow row = grvAssigned.Rows[index];

            if (e.CommandName.Equals("DropChecklist"))
            {
                try
                {
                    String component = GridViewOperator.GetCellStringValue(grvAssigned, row, "component");
                    ChecklistOperator.DropAssignedChecklist(component);
                }
                catch (Exception ex)
                {
                    Messenger.Show(Response, "Error: " + ex.Message);
                    return;
                }
            }

            RefreshForm();
        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            ChecklistOperator.Check(txtTaskID.Text.Trim());

            RefreshForm();
        }

        private void RefreshForm()
        {
            try
            {
                ChecklistOperator.RefreshAssignedGridView(grvAssigned, txtTaskID.Text.Trim());
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
                return;
            }

            grvAvailable.DataBind();
            grvReport.DataBind();
        }
    }
}
